package com.bw.a2207a.view

import android.content.Intent
import android.view.View
import android.widget.ImageView
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.bw.a2207a.R
import com.bw.a2207a.base.BaseActivity
import com.bw.a2207a.databinding.ActivityGoodsDetail2Binding
import com.bw.a2207a.util.GlideUtil
import com.bw.a2207a.viewmodel.GoodsDetailViewModel

/**
 * 带悬浮视频播放功能的 商品详情页面
 * @property adapter CommentAdapter
 * @property datas MutableList<CommentBean>
 */
@Route(path = "/main/GoodsDetailActivity2")
class GoodsDetailActivity2 :
    BaseActivity<ActivityGoodsDetail2Binding, GoodsDetailViewModel>(R.layout.activity_goods_detail2) {

    override fun initData() {
        viewModel.goodsAddLiveData.observe(this) {
            ToastUtils.showShort(it.message)
        }

        val banners = mutableListOf<String>()
        //设置商品banner
        viewModel.goodsLiveData.observe(this) {
            banners.add("http://ali2.a.yximgs.com/upic/2020/09/02/22/BMjAyMDA5MDIyMjAwNDZfNjU0Njc0XzM1NDM5MTg2Mjg3XzFfMw==_low_B1d223c4240dff71a58d062c26331ab9e.webp?tag=1-1599265874-vdg-0-w10q14bbdx-24bc3d6488ad4b86&bp=10000")
            banners.addAll(it.data.bannerList)

            binding.detailBanner.setData(banners, null)
            binding.detailBanner.setmAdapter { banner, model, view, position ->
                GlideUtil.loadImg(
                    this,
                    banners.get(position),
                    view as ImageView
                )
            }
            //设置banner点击事件
//            binding.detailBanner.setOnItemClickListener(object : XBanner.OnItemClickListener {
//                override fun onItemClick(
//                    banner: XBanner?,
//                    model: Any?,
//                    view: View?,
//                    position: Int
//                ) {
//
//                    //跳转到视频播放页面
//                    startActivity(Intent(this@GoodsDetailActivity2, VideoActivity::class.java))
//                }
//
//            })

            //设置价格
            binding.tvPrice.text = "¥ ${it.data.goods_default_price}"
            //设置商品描述信息
            binding.tvDesc.text = it.data.goods_desc


        }
    }

    override fun initView() {
        val id = intent.getIntExtra("id", 0)
        viewModel.getGoodsDetail(id)

        //设置视频播放地址
        binding.video.setVideoPath("http://txmov2.a.yximgs.com/upic/2020/09/02/22/BMjAyMDA5MDIyMjAwNDZfNjU0Njc0XzM1NDM5MTg2Mjg3XzFfMw==_b_B3e4fad299fd832fc75ba67df44acb80b.mp4?tag=1-1599265874-vdg-1-r8rhfdew2y-62c7c39afc8eee5a&tt=b&bp=10000")

        //设置视频快进暂停 按钮
//        binding.video.setMediaController(MediaController(this))
        binding.video.start()

        //点击关闭按钮，隐藏视频view
        binding.tvClose.setOnClickListener {
            binding.video.stopPlayback()
            binding.flVp.visibility = View.GONE
        }
        //点击视频view ，跳转视频播放页面
        binding.flVp.setOnClickListener {
            //跳转到视频播放页面
            startActivity(Intent(this@GoodsDetailActivity2, VideoActivity::class.java))
        }

        //添加购物车
        binding.tvAdd.setOnClickListener {
            viewModel.addCar(id)
        }
        //跳转到购物车列表
        binding.tvBuy.setOnClickListener {
            // viewModel.addCar(id)
            startActivity(Intent(this@GoodsDetailActivity2, CartListActivity::class.java))
        }
    }


}